#include <iostream>
using namespace std;

const int col = 7;
enum dir
{
    Asc,
    Desc
};

inline void intro()
{
    cout << endl
         << "学号：3170608043，姓名：石英钰" << endl
         << endl
         << "软件17有2个班，共20个学生，每个学生有学号、班级号、4门课程成绩。计算每个学生的总成绩，每门课程的平均成绩，设计一个按二维数组的某列排序的函数，按学生课程的总成绩分班级排序" << endl
         << endl;
}

void sort(int a[][col], int n, int Cn, dir D)
{
    int t[col];
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if ((a[i][Cn] < a[j][Cn] && D == Desc) || (a[i][Cn] > a[j][Cn] && D == Asc))
            {
                memcpy(t, a[i], sizeof(t));
                memcpy(a[i], a[j], sizeof(t));
                memcpy(a[j], t, sizeof(t));
            }
        }
    }
}

int main()
{
    intro();
    const int CourseN = col;
    int n, score[20][CourseN] = {
               {20140101, 1, 82, 86, 93, 76, 0},
               {20140203, 2, 80, 80, 86, 90, 0},
               {20140204, 2, 86, 90, 80, 86, 0},
               {20140205, 2, 90, 83, 71, 60, 0},
               {20140102, 1, 75, 86, 92, 82, 0},
               {20140104, 1, 82, 86, 86, 92, 0},
               {20140103, 2, 80, 80, 90, 80, 0},
               {20140207, 2, 86, 90, 86, 90, 0},
               {20140206, 2, 90, 83, 69, 84, 0},
               {20140110, 1, 75, 86, 84, 72, 0},
               {20140109, 1, 82, 86, 83, 59, 0},
               {20140209, 2, 80, 80, 82, 77, 0},
               {20140202, 2, 86, 90, 80, 76, 0},
               {20140201, 2, 90, 83, 73, 73, 0},
               {20140105, 1, 75, 86, 76, 80, 0},
               {20140103, 1, 82, 86, 90, 70, 0},
               {20140210, 2, 80, 80, 67, 78, 0},
               {20140107, 1, 86, 90, 82, 78, 0},
               {20140208, 2, 90, 83, 63, 68, 0},
               {20140108, 1, 75, 86, 83, 69, 0}};

    double arrange[5];
    n = sizeof(score) / sizeof(score[0]);
    for (int i = 0; i < n; i++)
    {
        for (int j = 2; j < CourseN - 1; j++)
        {
            score[i][CourseN - 1] += score[i][j];
            arrange[j - 2] += score[i][j];
        }
        arrange[4] += score[i][CourseN - 1];
    }

    sort(score, n, 6, Desc);
    sort(score, n, 1, Asc);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < CourseN; j++)
        {
            cout << score[i][j] << '\t';
        }
        cout << endl;
    }
    cout << "---------------------------------------------------\narrange\t\t\t";
    for (int i = 0; i < sizeof(arrange) / sizeof(arrange[0]); i++)
    {
        cout << arrange[i] / n << "\t";
    }
    cout << endl;

    return 0;
}
